import { createStore } from 'vuex'

export default createStore({
  state: {
    // 当前音乐播放列表
    musicPlayList: [],

    // 当前播放歌曲的下标
    currentIndex: 0,

    // 播放状态 ： true播放 false暂停
    playing: false,

    // 播放模式: 0 顺序播放  1单曲循环  2随机播放  4列表循环
    playMode: 0,

    // 全屏播放 true / false展示底部播放条
    fullScreen: false
  },
  getters: {
    // 当前播放的歌曲对象
    currentSong(state) {
      return state.musicPlayList[state.currentIndex] || {}
    }
  },
  mutations: {
    // 设置当前的歌曲播放列表
    setMusicPlayList(state, list) {
      state.musicPlayList = list
    },
    // 设置当前播放歌曲的下标
    setCurrentIndex(state, index) {
      state.currentIndex = index
    },
    // 设置播放状态
    setPlaying(state, isPlayState) {
      state.playing = isPlayState
    },
    // 设置播放模式
    setPlayMode(state, mode) {
      state.playMode = mode
    },
    // 设置全屏播放的状态
    setFullScreen(state, isFullScreenState) {
      state.fullScreen = isFullScreenState
    }
  },
  actions: {
    // 添加1首歌到播放列表
    addOneSong({ state, commit }, song) {
      console.log('addsong',song);
      
      // 打开全屏播放
      commit('setFullScreen', true)

      // console.log("接收的歌曲", song);
      let { musicPlayList, currentIndex } = state

      musicPlayList.push(song)

      currentIndex++

      // 限制边界
      if (musicPlayList.length === 1) currentIndex = 0

      // 去除重复项
      // 添加的歌曲放在 当前播放歌曲的后一个位置

      commit('setMusicPlayList', musicPlayList)
      commit('setCurrentIndex', currentIndex)
    },
    addOneSong2({ state, commit }, song) {
      console.log('addsong2', song)
      // 打开全屏播放
      commit('setFullScreen', true)
      // console.log("接收的歌曲", song);
      let { musicPlayList, currentIndex } = state
      musicPlayList.push(song)

      currentIndex++

      // 限制边界
      if (musicPlayList.length === 1) currentIndex = 0

      // 去除重复项
      // 添加的歌曲放在 当前播放歌曲的后一个位置

      commit('setMusicPlayList', musicPlayList)
      commit('setCurrentIndex', currentIndex)
    }
  },
  modules: {}
})
